<template>
	<view class="page">
		<u-navbar bgColor="#E6A5B4" title="课表" placeholder :titleStyle="titleStyle">
			<template v-slot:left>
				<view class="leftTitleV">
					{{nameCity}}
					<u-icon name="arrow-down" color="#BCBBBC"></u-icon>
				</view>
			</template>
		</u-navbar>
		<view class="tabV">
			<view class="tabV1">
				<u-tabs :list="tabList" lineColor="#00000000" :activeStyle="activeStyle" :inactiveStyle="inactiveStyle"
					@change="tabChange" :current="current" />
			</view>
			<view v-if="current!=3" class="tabV2" @tap="onScreeningClick">
				筛选
				<image src="../../static/schedule/screening.svg"></image>
			</view>
		</view>
		<!-- 团课 -->
		<group v-if="current==0"></group>
		<!-- 私教 -->
		<teach v-if="current==1"></teach>
		<!-- 训练营 -->
		<training v-if="current==2"></training>
		<!-- 线上课 -->
		<online v-if="current==3" :thmeId="thmeId"></online>

		<ns-login ref="login" @saveUserInfo="onUserInfoClick"></ns-login>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				nameCity: '',
				current: 0,
				titleStyle: {
					"color": "#ffffff"
				},
				activeStyle: {
					"color": "#E6A5B4D",
					"font-size": "36rpx"
				},
				inactiveStyle: {
					"color": "#8D8D8D",
					"font-size": "32rpx"
				},
				tabList: [{
					name: '服务项目',
					id: 0
				}, {
					name: '专家团队',
					id: 1
				}, {
					name: '沙龙活动',
					id: 2
				}, {
					name: '健康管理',
					id: 3
				}],
				thmeId: "0", //主题id
			}
		},
		onLoad(e) {
			let index = uni.getStorageSync("router_to");
			if (index) {
				this.current = index;
				uni.setStorageSync("router_to", "");
			}
			this.getCity();
		},
		onShow() {
			let index = uni.getStorageSync("router_to");
			if (index) {
				this.current = index;
				uni.setStorageSync("router_to", "");
			}
			let thmeid = uni.getStorageSync("router_online_id");
			this.thmeId = thmeid;
			uni.setStorageSync("router_online_id", "");

			this.userInfo = uni.getStorageSync("user_info");
			if (this.userInfo) {
				this.$refs.login.close();
			} else {
				this.$refs.login.open({
					show: false
				});
			}
		},
		onReady() {

		},
		methods: {
			onUserInfoClick(e) {
				if (e.code == 200) {

				}
			},
			getCity() {
				let that = this;
				uni.authorize({
					scope: 'scope.userLocation',
					success() {
						uni.getLocation({
							type: 'gcj02',
							geocode: true, //设置该参数为true可直接获取经纬度及城市信息
							success: function(res) {
								that.getCityName(res.latitude, res.longitude);
								uni.setStorageSync("user_longitude", res.longitude);
								uni.setStorageSync("user_latitude", res.latitude);
							},
							fail(res) {
								console.log("定位失败--" + JSON.stringify(res))
							}
						});
					},
					fail() {
						uni.showModal({
							title: '未打开小程序定位',
							content: '找不到您的位置，请开启定位',
							confirmText: '开启定位',
							showCancel: false,
							success: (res) => {
								if (res.confirm) {
									uni.openSetting(); // 打开地图权限设置
								}
							}
						});
					}
				})
			},
			getCityName(lat, log) {
				this.$api.getCityName({
					latitude: lat,
					longitude: log
				}).then((res) => {
					this.nameCity = res.city;
				}).catch((e) => {})
			},
			tabChange(index) {
				this.current = index.id
			},
			getEducationList() {
				console.log("======")
				this.$api.getEducationList({
					longitude: 113.489411,
					latitude: 22.944922,
					combo_ids: 1,
					day: 1.14,
					shop_ids: "1,2"
				}).then((res) => {

				}).catch((e) => {
					uni.$u.toast(e.msg === undefined ? "请求失败" : e.msg)
				})
			},
			onScreeningClick() {
				if (this.current == 1) {
					uni.$emit('Teach', {
						id: '私教'
					});
				} else if (this.current == 2) {
					uni.$emit('Training', {
						id: '训练营'
					});
				} else if (this.current == 0) {
					uni.$emit('Screening', {
						id: '团课'
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	.leftTitleV {
		font-size: 32rpx;
		color: #FFFFFF;
		display: flex;
	}

	.tabV {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-right: 30rpx;

		.tabV1 {
			flex: 1;
		}

		.tabV2 {
			font-size: 28rpx;
			color: #8D8D8D;
			display: flex;
			align-items: center;
			justify-content: center;

			image {
				width: 30rpx;
				height: 30rpx;
				margin-left: 10rpx;
			}
		}
	}
</style>